package main;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Observable;
import java.util.Scanner;
import java.util.StringTokenizer;

public class UserInterface extends Observable{
	public UserInterface() {
		
	}
	
	public void startUI() {		
		Scanner scan = new Scanner(System.in);
		int numPages = 0;
		List<String> keywords = new LinkedList<String>();
		String keywordList;
		String URL = "";
		StartUpContainer mySUC;
		boolean ok = false;
		
		System.out.println("Welcome to the Webcrawler.");
		
		//get number of pages to traverse.
		while(!ok) {
			System.out.println("Please enter the number of pages to traverse: ");
			
			numPages = scan.nextInt();
			if (numPages < 1) {
				System.out.println("Input must be greater than 0");
			} 
			else {
				ok = true;
			}	
		}
		ok = false;
		
		//get keywords
		
		while(!ok) {
			System.out.println("Please enter a list of keywords to search for, up to ten, seperated by spaces: ");
			scan = new Scanner(System.in);
			
			//get keywords
			keywordList = scan.nextLine();
			StringTokenizer st = new StringTokenizer(keywordList);
			
			int i=0;
			
			while(st.hasMoreTokens()) {
				keywords.add(st.nextToken(" "));
			}
			
			//make sure # of keywords are greater than 0 but less than 11.
			if (keywords.size() > 0 && keywords.size() < 11) {
				ok = true;
			}
		}

		ok = false;
		scan = new Scanner(System.in);
		
		while(!ok) {
			System.out.println("Please enter the starting url: ");
			URL = scan.nextLine();
			if(!URL.isEmpty()) {
				ok = true;
			}
		}
		
		mySUC = new StartUpContainer(keywords, URL, numPages);
		setChanged();
		notifyObservers(mySUC);
	}
	
	public void statsUI(final StatisticsContainer mySC) {
		StringBuilder sb = new StringBuilder();
		
		sb.append("\n\n");
		
		sb.append("Pages Retrieved:  ");
		sb.append(mySC.getNumberTotalPagesCrawled());
		sb.append("\n");
		
		sb.append("Average words per page: ");
		sb.append(mySC.getMyAverageWordsPerPage());
		sb.append("\n");
		
		sb.append("Average URLs per page: ");
		sb.append(mySC.getMyAverageNumURLPerPage());
		sb.append("\n");
		
		sb.append(String.format("%1$10s  %2$5s  %3$10s  %4$5s %5$10s\n", "Keyword", " ", 
				"Avg. hits per page", " ", "Total hits"));
		
		for(KeywordContainer ksc: mySC.getKeywords()) {
			sb.append(String.format("%1$10s  %2$5s %3$10s  %4$10s %5$10s\n", ksc.getMyKeyword(),
					" ", roundThreeDecimals(ksc.getAverageOccurences()), " ", ksc.getTotalOccurences()));
		}
		
		sb.append("Average parse time per pages; ");
		sb.append(mySC.getMyAverageTimePerPage());
		sb.append(" sec");
		sb.append("\n");
		
		sb.append("Total running time: ");
		sb.append(mySC.getMyTotalExecutionTime());
		sb.append(" sec");
		
		System.out.println(sb.toString());
		System.out.println("Done");
		
		
	}
	
	
	
	private double roundThreeDecimals(final double theNumber) {
		DecimalFormat numFormat = new DecimalFormat("0.000");
		return Double.valueOf(numFormat.format(theNumber));
	}	
}
